/*
 * Copyright (c) 2023 Huawei Technologies Co., Ltd.
 * openUBMC is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *         http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PSL v2 for more details.
 */

#include "interface/pcie_device.h"
#include "mc/format.h"
#include "mc/string.h"

namespace dev {

bool PCIeDevice::SetRootBDF(uint8_t bus, uint8_t device, uint8_t function) {
    if (bus == 0 && device == 0 && function == 0) {
        std::cerr << "Error: Invalid BDF values" << std::endl;
        return false;
    }
    Bus      = bus;
    Device   = device;
    Function = function;

    return true;
}

bool PCIeDevice::SetDeviceBDF(uint8_t bus, uint8_t device, uint8_t function) {
    if (bus == 0 && device == 0 && function == 0) {
        std::cerr << "Error: Invalid BDF values" << std::endl;
        return false;
    }

    DevBus      = bus;
    DevDevice   = device;
    DevFunction = function;

    post([this, bus, device, function]() {
        pcie_device_bdf_changed(bus, device, function);
    });

    return true;
}

void PCIeDevice::replace_dynamic_property(mc::mutable_dict& object, const mc::dict& connector) {
    mc::mutable_dict pcie_device_obj = object["bmc.dev.PCIeDevice"].as<mc::mutable_dict>();
    if (pcie_device_obj.contains("Slot")) {
        if (pcie_device_obj["Slot"].as<std::string>() == "${Slot}") {
            pcie_device_obj["Slot"] = connector["Slot"].as<uint8_t>();
        }
    }
    if (pcie_device_obj.contains("DeviceName")) {
        std::string device_name = pcie_device_obj["DeviceName"].as<std::string>();
        if (device_name.find("${Slot}") != std::string::npos) {
            mc::fmt::format_dict(device_name, device_name, connector);
            pcie_device_obj["DeviceName"] = device_name;
        }
    }
    object["bmc.dev.PCIeDevice"] = pcie_device_obj;
}
bool PCIeDevice::update_pcie_device_id_by_ncsi() {
    ncsi_version_id_info_t version_id = {};
    bool ret = m_ncsi_over_mctp_huawei->get_version_id(DEFAULT_PACKAGE_ID, DEFAULT_CHANNEL_ID,
                                                       &version_id);
    if (!ret) {
        elog("Error: Failed to update version id");
        return false;
    }

    if (VendorId == "") {
        VendorId.set_value(version_id.vid);
    }
    if (DeviceId == "") {
        DeviceId.set_value(version_id.did);
    }
    if (SubSystemVendorId == "") {
        SubSystemVendorId.set_value(version_id.svid);
    }
    if (SubSystemDeviceId == "") {
        SubSystemDeviceId.set_value(version_id.sdid);
    }
    return true;
}

void PCIeDevice::start_ncsi_update_task_huawei(ncsi_over_mctp_hw_ptr ncsi_over_mctp,
                                               mc::milliseconds      interval) {
    m_ncsi_timer            = new mc::core::timer(this);
    m_ncsi_over_mctp_huawei = ncsi_over_mctp;
    m_ncsi_timer->timeout.connect([this]() {
        update_pcie_device_id_by_ncsi();
    });
    m_ncsi_timer->set_single_shot(false);
    m_ncsi_timer->start(interval);
}

void PCIeDevice::stop_ncsi_update_task() {
    if (m_ncsi_timer) {
        m_ncsi_timer->stop();
        delete m_ncsi_timer;
        m_ncsi_timer = nullptr;
    }
    if (m_ncsi_over_mctp_huawei) {
        m_ncsi_over_mctp_huawei = nullptr;
    }
}

} // namespace dev

MC_REFLECT(dev::PCIeDevice)